package io.renren.modules.sp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.constant.Constant;
import io.renren.modules.sp.dao.SpTicketDao;
import io.renren.modules.sp.dto.SpTicketDTO;
import io.renren.modules.sp.entity.SpTicketEntity;
import io.renren.modules.sp.service.SpMatchItemTeamService;
import io.renren.modules.sp.service.SpTicketService;
import io.renren.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参赛证表
 *
 * @author whc 15953295779@126.com
 * @since 3.0 2023-04-21
 */
@Service
public class SpTicketServiceImpl extends CrudServiceImpl<SpTicketDao, SpTicketEntity, SpTicketDTO> implements SpTicketService {

    @Autowired
    private SpMatchItemTeamService spMatchItemTeamService;

    @Override
    public QueryWrapper<SpTicketEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<SpTicketEntity> wrapper = new QueryWrapper<>();

        String matchId = (String)params.get("matchId");
        wrapper.eq(StringUtils.isNotBlank(matchId), "match_id", matchId);
        String teamUserId = (String)params.get("teamUserId");
        wrapper.eq(StringUtils.isNotBlank(teamUserId), "team_user_id", teamUserId);

        return wrapper;
    }


    @Override
    public PageData<Map> queryTicketBy(Map<String, Object> params){
        Page page = createPage(params);
        //分页参数
        params.put(Constant.PAGE, page);
        IPage<Map<String,Object>> page2 = this.baseDao.queryTicketBy(page, params);
        PageData<Map> pageData = new PageData(page2.getRecords(),page2.getTotal());
        return pageData;
    }

    @Override
    public void deleteByTeamId(Long teamId){
        //根据team_id删除
        QueryWrapper<SpTicketEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id", teamId);
        this.baseDao.delete(wrapper);
    }

    @Override
    public void generateCertificatePic(Map<String, Object> params){
        long size = 20000;
        String sizeStr = (String) params.get("size");
        if(StringUtils.isNotBlank(sizeStr)){
            size = Long.parseLong(sizeStr);
        }

        Page page = new Page(1, size);
        //Map<String, Object> params = new HashMap<>();
        params.put("matchId", 67);
        params.put("url", "is not null");
        //params.put("teamName", "甘肃省代表队");
        //甘肃省代表队   柳州
        //params.put("userId", 25745); //1206  3205
        //查询证件url为空的记录
        List<Map<String,Object>> ticketList = this.baseDao.queryTicketBy(page, params).getRecords();
        //遍历记录，生成证件图
        String ticket = "";
        SpTicketEntity ticketEntity = null;
        for (Map<String,Object> map : ticketList){
            ticketEntity = new SpTicketEntity();
            ticket = map.get("ticket").toString();
            ticketEntity.setId(Long.parseLong(map.get("id").toString()));
            ticketEntity.setTicket(ticket);
            ticketEntity.setMatchId(Long.parseLong(map.get("matchId")+""));
            ticketEntity.setMatchName(map.get("matchName").toString());
            String str = map.get("teamName").toString();
            // teamName 截取字符串中最后一个括号之前的内容
            if(StringUtils.isNotBlank(str)){
                str = str.replaceAll("\\(", "（");
                str = str.replaceAll("\\)", "）");
                // 校验规则：最后一个）后没有其他内容，去掉最后一对（）内的内容;
                if(str.lastIndexOf("）") == str.length() - 1) {
                    str = str.substring(0, str.lastIndexOf("（"));
                }
            }
            ticketEntity.setTeamName(str);
            ticketEntity.setUserId(Long.parseLong(map.get("userId")+""));
            ticketEntity.setMatchId((Long)map.get("matchId"));
            ticketEntity.setTeamId(Long.parseLong(map.get("teamId")+""));
            ticketEntity.setPic(map.get("pic").toString());
            ticketEntity.setPic2(map.get("pic2").toString());
            ticketEntity.setPic3(map.get("pic3").toString());
            //生成证件图
            if(ticket.contains("L")){
                spMatchItemTeamService.genAndSaveTicketUrl(ticketEntity, "1", "1");
            }else{
                spMatchItemTeamService.genAndSaveTicketUrl(ticketEntity, "2", "3");
            }
        }

    }


    @Override
    public void generateHaomabuCertificatePic(Map<String, Object> params){
        long size = 20000;
        String sizeStr = (String) params.get("size");
        if(StringUtils.isNotBlank(sizeStr)){
            size = Long.parseLong(sizeStr);
        }

        Page page = new Page(1, size);
        //Map<String, Object> params = new HashMap<>();
        params.put("matchId", 67);
        params.put("numberPic", "is not null");
        //params.put("teamName", "甘肃省代表队");
        //甘肃省代表队   柳州
        //params.put("userId", 25745); //1206  3205
        //查询证件url为空的记录
        List<Map<String,Object>> ticketList = this.baseDao.queryTicketBy(page, params).getRecords();
        //遍历记录，生成证件图
        String ticket = "";
        SpTicketEntity ticketEntity = null;
        for (Map<String,Object> map : ticketList){
            ticketEntity = new SpTicketEntity();
            ticket = map.get("ticket").toString();
            ticketEntity.setId(Long.parseLong(map.get("id").toString()));
            ticketEntity.setTicket(ticket);
            ticketEntity.setMatchId(Long.parseLong(map.get("matchId")+""));
            ticketEntity.setMatchName(map.get("matchName").toString());
            String str = map.get("teamName").toString();
            // teamName 截取字符串中最后一个括号之前的内容
            if(StringUtils.isNotBlank(str)){
                str = str.replaceAll("\\(", "（");
                str = str.replaceAll("\\)", "）");
                // 校验规则：最后一个）后没有其他内容，去掉最后一对（）内的内容;
                if(str.lastIndexOf("）") == str.length() - 1) {
                    str = str.substring(0, str.lastIndexOf("（"));
                }
            }
            ticketEntity.setTeamName(str);
            ticketEntity.setUserId(Long.parseLong(map.get("userId")+""));
            ticketEntity.setMatchId((Long)map.get("matchId"));
            ticketEntity.setTeamId(Long.parseLong(map.get("teamId")+""));
            ticketEntity.setPic(map.get("pic").toString());
            ticketEntity.setPic2(map.get("pic2").toString());
            ticketEntity.setPic3(map.get("pic3").toString());

            //号码布
            spMatchItemTeamService.genAndSaveTicketUrl(ticketEntity, "2", "2");

        }

    }


}
